import { session } from 'js/util'
import app from '../main'

export default {
  state: {
    userInfo: (session.get('userInfo') || {})
  },
  getters: {
    token: state => (state.userInfo.token ? state.userInfo.token.accessToken : ''),
    userName: state => (state.userInfo.user ? state.userInfo.user.userName : '') 
  },
  actions: {
  },
  mutations: {
    // 更新用户信息
    updateUserInfo(state, payload) {
      session.set('userInfo', payload)
      state.userInfo = payload
    },
    // 清除用户信息
    clearUserInfo(state) {
      app.$toast('已成功退出登录！', () => {
        app.$router.push('/login')
        session.remove('userInfo')
        session.remove('curMenu')
        session.remove('curMenuIndex')
        session.remove('curMenuTitle')
        state.userInfo = {}
      })
    }
  }
}
